<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>js数组之展开元素</title>
</head>
<body>
  <ul>
    <li>合并多个数组，以代替之前循环push</li>
    <li>为函数声明可变参数，以代替之前多参数重载</li>
    <li>将DOM元素转化为数组，完成后续的相关操作</li>
  </ul>
  <script>
    const arr1 = [1, 2, 3, 4]
    const arr2 = ['Miracle', 'Miracle He']
    console.log([...arr1, ...arr2]) // [1, 2, 3, 4, "Miracle", "Miracle He"]
    const sum = function (...args) {
      return args.reduce((p, c) => p += c, 0)
    }
    console.log(sum(1, 2))          // 3
    console.log(sum(1, 2, 3, 4))    // 10
    const lis = document.querySelectorAll('li')
    [...lis].map(function (li) {
      li.addEventListener('click', function () {
        this.style.backgroundColor = 'red'
      })
    })
  </script>
</body>
</html>